using Microsoft.Phone.BackgroundAudio;
using WindowsPhoneAudioStreaming.Audio.Streaming;
using WindowsPhoneAudioStreaming.Audio.Streaming.Agent;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.IO;

namespace Samples.SimpleStreaming.Agent {
    public class SimpleAudioPlayerService : IAudioPlayerService {

        public IPlaybackOptions PlaybackOptions { get { return Options.LoadOptions(); } }

        public int NumberOfTracks {
            get { return 1; }
        }

        public AudioTrack TrackByIndex(int trackIndex) {
            //return new AudioTrack(null, "Test stream", "Sample", "Sample Author", null, "http://icecast.arn.com.au/1011.mp3", EnabledPlayerControls.Pause);
            return new AudioTrack(null, "Test stream", "Sample", "Sample Author", null, "http://radio1.internode.on.net:8002/62", EnabledPlayerControls.Pause);
        }

        public int NextTrackIndex(int currentTrackIndex) {
            return -1;
        }

        public int PreviousTrackIndex(int currentTrackIndex) {
            return -1;
        }

        public void UpdatePlaylist(bool includeMusicAndVideoHub = false) {
            // do nothing
        }



        public class Options : IPlaybackOptions {
            private const string OptionsFilename = "Streaming.Options.txt";

            public bool EnableIcyMetaData { get; set; }
            public bool WiFiAndEthernetOnly { get; set; }
            public int BufferLengthInSeconds { get; set; }
            public int LogUpdateInSeconds { get; set; }
            public int CellularStreamingThreshold { get; set; }

            public static Options LoadOptions() {
                Options options;
                var serializer = new DataContractSerializer(typeof(Options));

                try {
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
                        if (store.FileExists(OptionsFilename)) {
                            using (var strm = store.OpenFile(OptionsFilename, FileMode.Open, FileAccess.Read)) {
                                options = serializer.ReadObject(strm) as Options;
                            }
                        }
                        else {
                            options = new Options() {
                                BufferLengthInSeconds = 10,
                                LogUpdateInSeconds = 20,
                                CellularStreamingThreshold = 1000000
                            };
                        }
                    }
                }
                catch {
                    options = new Options() {
                        BufferLengthInSeconds = 10,
                        LogUpdateInSeconds = 20,
                        CellularStreamingThreshold = 1000000
                    };
                }
                return options;
            }

            public void SaveOptions(Options options) {
                var serializer = new DataContractSerializer(typeof(Options));

                using (var strm = new MemoryStream()) {
                    serializer.WriteObject(strm, options);

                    strm.Flush();
                    strm.Position = 0;
                    using (var reader = new StreamReader(strm)) {
                        var serialized = reader.ReadToEnd();
                        Debug.WriteLine(serialized);
                    }
                }

                using (var strm = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(OptionsFilename, FileMode.Create, FileAccess.Write)) {
                    serializer.WriteObject(strm, options);
                }
            }
        }

    }
}